package network.URL;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class URLTest {

    public static void main(String[] args) throws IOException {
        //1.下载地址
        URL url = new URL("https://i1.hdslb.com/bfs/face/e3637ff7fa559e53a6bd4a8521b9446619f00cad.jpg@160w_160h_1c_1s.webp");
        //下载什么格式的文件，则文件后缀名需要对应上
        String fileName = "46619f00cad.jpg";

        //2.连接到该资源 HTTP
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream(fileName);

        byte[] buffer = new byte[1024];
        int len;
        while((len=inputStream.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }


        //关闭资源
        fos.close();
        inputStream.close();
        urlConnection.disconnect();
    }

}
